﻿using CS.BLL;
using CS.model;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace chaoshiguanli
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }

        private void Form2_KeyUp(object sender, KeyEventArgs e)
        {
            if(e.KeyCode == Keys.F2)
            {
                txtshuru.Focus();
            }
            else if (e.KeyCode == Keys.F5)
            {
                button2_Click(null,null);
            }
            else if(e.KeyCode==Keys.F1)
            {
                dgvGoods.Focus();
            }
            else if (e.KeyCode == Keys.Escape)
            {
                this.Close();
            }
        }

        private GetAll mm = new GetAll();
        public List<BuyGoods> Goods = new List<BuyGoods>();
        private void button1_Click(object sender, EventArgs e)
        {
            if (txtshuru.Text != "")
            {
                List<shangp> g = mm.anshfd(txtshuru.Text);
                BuyGoods item=new BuyGoods();
                if (g.Count == 0)
                {
                    MessageBox.Show("商品编码输入错误");
                }
                else if (g.Count == 1)
                {
                    item.GoodsName = g[0].GoodsName;
                    item.SalePrice = g[0].SalePrice;
                    item.BuyCount = 1;
                    Goods.Add(item);
                }
                else
                {
                    BuyGoodsInfo bg = new BuyGoodsInfo(null);
                    bg.showDataList = g;
                    bg.ShowDialog();
                    shangp sp = bg.SelectGoods;

                    item.GoodsName = sp.GoodsName;
                    item.SalePrice = sp.SalePrice;
                    item.BuyCount = 1;
                    Goods.Add(item);
                }
                shux();
                txtshuru.Text = "";
            }
        }

        private void shux()
        {
            if (Goods.Count == 0) { btnjiesuan.Enabled = false; } else { btnjiesuan.Enabled = true; }
            dgvGoods.DataSource = new BindingList<BuyGoods>(Goods);//绑定数据
            int totalCount = 0;
            double totalMoney = 0.0;
            foreach (BuyGoods item in Goods)
            {
                totalCount += item.BuyCount;
                totalMoney += item.XiaoJi;
            }
            labmoney.Text = totalMoney.ToString();
            labliang.Text=totalCount.ToString();
        }
        private void txtshuru_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                if (txtshuru.Text != "")
                {
                    button1_Click(null, null);
                }
            }

        }

        private void Form2_Load(object sender, EventArgs e)
        {
            dgvGoods.AutoGenerateColumns = false;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (Goods.Count>0)
            {
                JieSuan ju = new JieSuan();
                ju.Buylist = Goods;
                ju.ShowDialog();
                shux();
            }
        }

        private void dataGridView1_KeyUp(object sender, KeyEventArgs e)
        {
            if(e.KeyCode==Keys.Enter){
                shux();
            }else if(e.KeyCode==Keys.Delete)
            {
                int index = dgvGoods.SelectedCells[0].OwningRow.Index;
                Goods.RemoveAt(index);
                shux();
            }
        }
    }
}
